package com.sicau.socialpracticesystem.controller;


import com.github.dozermapper.core.Mapper;
import com.sicau.socialpracticesystem.common.constant.UrlConstants;
import com.sicau.socialpracticesystem.common.returning.ResultData;
import com.sicau.socialpracticesystem.config.aop.ValidateAspect;
import com.sicau.socialpracticesystem.entity.param.teaminstruction.TeamInstructionAddParam;
import com.sicau.socialpracticesystem.entity.param.teaminstruction.TeamInstructionQueryParam;
import com.sicau.socialpracticesystem.entity.param.teaminstruction.TeamInstructionUpdateParam;
import com.sicau.socialpracticesystem.entity.po.TeamInstruction;
import com.sicau.socialpracticesystem.entity.vo.PageDataVO;
import com.sicau.socialpracticesystem.entity.vo.teaminstruction.TeamInstructionVO;
import com.sicau.socialpracticesystem.service.TeamInstructionService;
import com.sicau.socialpracticesystem.util.UserContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * 支队介绍 前端控制器
 * </p>
 *
 * @author sxt && ttxxi
 * @since 2021-04-26
 */
@RestController
@Api(tags = "支队介绍相关接口")
@PreAuthorize("isAuthenticated()")
@RequestMapping(UrlConstants.TEAM_INSTRUCTION_PREFIX)
public class TeamInstructionController {

    @Resource
    private Mapper dozerMapper;

    @Resource
    private TeamInstructionService teamInstructionService;

    @PostMapping
    @ApiOperation("添加一个支队介绍")
    @ValidateAspect.ParamValidate()
    public ResultData<Object> addPracticeType(@RequestBody @Valid TeamInstructionAddParam teamAddParam,
                                              @ApiParam(hidden = true)
                                              @SuppressWarnings("unused") BindingResult bindingResult) {

        TeamInstruction teamInstruction = dozerMapper.map(teamAddParam, TeamInstruction.class);
        teamInstruction.setCreator(UserContext.get().getId());
        teamInstruction.setCreateTime(LocalDateTime.now());
        return teamInstructionService.save(teamInstruction)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED();
    }

    @GetMapping
    @PreAuthorize("permitAll()")
    @ApiOperation("查询支队介绍(带分页)")
    public ResultData<PageDataVO<TeamInstructionVO>> getPrizes(TeamInstructionQueryParam teamInstructionQueryParam) {
        PageDataVO<TeamInstructionVO> teamInstructionVOPageDataVO = teamInstructionService.queryTeamInstructionByConditions(teamInstructionQueryParam);
        return new ResultData<PageDataVO<TeamInstructionVO>>().OK().setObj(teamInstructionVOPageDataVO);
    }

    @GetMapping("/{teamId}")
    @PreAuthorize("permitAll()")
    @ApiOperation("查询支队介绍")
    public ResultData<TeamInstructionVO> getPrizes(@PathVariable Integer teamId) {
        TeamInstructionVO teamInstructionVO = teamInstructionService.queryTeamInstructionByTeamId(teamId);
        if (teamInstructionVO == null){
            return new ResultData().NOT_MATCH();
        }
        return new ResultData<TeamInstructionVO>().OK().setObj(teamInstructionVO);
    }

    @DeleteMapping
    @ApiOperation("删除一个或多个支队介绍")
    public ResultData<Object> removePrizes(@RequestParam("ids") ArrayList<Integer> ids) {
        return teamInstructionService.removeByIds(ids)
                ? new ResultData<>().OK()
                : new ResultData<>().FAILED().setResultIns("部分或全部数据删除失败, 可能有的数据已经被删除了");
    }

    @PutMapping("/{id}")
    @ApiOperation("修改支队介绍")
    @ValidateAspect.ParamValidate()
    public ResultData<Object> updatePrize(@PathVariable String id,
                                          @RequestBody @Valid TeamInstructionUpdateParam teamInstructionUpdateParam,
                                          @ApiParam(hidden = true)
                                          @SuppressWarnings("unused") BindingResult bindingResult) {
        TeamInstruction teamInstruction = teamInstructionService.getById(id);
        dozerMapper.map(teamInstructionUpdateParam, teamInstruction);
        return teamInstructionService.updateById(teamInstruction)
                ? new ResultData<>().OK().setObj(teamInstructionService.getById(id))
                : new ResultData<>().FAILED();
    }

}

